<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta content="IE=edge" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1, shrink-to-fit=no" name="viewport">
    <link href="/static/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="/static/vendor/skin/ftskin.css" rel="stylesheet">
    <title>添加(更新)法律法规</title>
    <style>
        .fram_seting {
            cursor: pointer;
        }
    </style>
</head>

<body style="margin-bottom: 60px;">

<div class="ft-subpage-wrapper container-fluid">
    <form action="#" id="form1" method="post" name="form1">
        <div class="row">
            <div class="col-lg-12" id="form_table">
                <fieldset>
                    <legend id="titleText">-添加(更新)政策法规-</legend>
                    <table cellpadding="0" cellspacing="0" class="form-table form-table-border" id="">
                        <tr>
                            <th><span class="f_red">*</span>标题</th>
                            <td colspan="3">
                                <input autofocus="autofocus" class="form-control"
                                       id="title" name="title"
                                       placeholder="请输入标题" style="text-align: center;font-size: 20px;font-weight: bold" type="text">
                                <span class="cError"></span>
                            </td>
                        </tr>
                        <tr>
                            <th><span class="f_red">*</span>内容</th>
                            <td colspan="3">
                                &nbsp;&nbsp;&nbsp;&nbsp;
                                <label>
                                    <i class="fram_seting A glyphicon glyphicon-font"></i>
                                    <select  onchange="setFontSize()">
                                        <option selected  disabled>  </option>
                                        <option  value="1">1</option>
                                        <option  value="2">2</option>
                                        <option  value="3">3</option>
                                        <option  value="4">4</option>
                                        <option  value="5">5</option>
                                        <option  value="6">6</option>
                                        <option  value="7">7</option>
                                    </select>
                                </label>
                                &nbsp;&nbsp;&nbsp;&nbsp;
                                <i class="fram_seting B glyphicon glyphicon-bold"></i>&nbsp;&nbsp;&nbsp;&nbsp;
                                <i class="fram_seting I glyphicon glyphicon-italic"></i>&nbsp;&nbsp;&nbsp;&nbsp;
                                <i class="fram_seting picture glyphicon glyphicon-picture"></i>&nbsp;&nbsp;&nbsp;&nbsp;
                                <span style="font-size: 12px;color: red">*为了防止浏览器关闭导致编辑的内容丢失，建议您先使用word编辑好，再粘贴进来...</span>
                                <iframe class="form-control" contenteditable="true" id="content"
                                        name="content" style="height: 500px;resize: none">
                                </iframe>
                                <span class="cError"></span>
                            </td>
                        </tr>
                        <tr>
                            <th>备注</th>
                            <td colspan="3">
                                <input class="form-control" id="remark" name="remark" placeholder="" type="text"
                                       value="">
                                <span class="cError"></span>
                            </td>
                        </tr>

                        <tr>
                            <th><span class="f_red">*</span>发布方式</th>
                            <td>
                                <label onclick="releaseMethod(1)">
                                    <input checked="checked" class="form-control btn"
                                           id="releaseMethod_1"
                                           name="releaseMethod" placeholder=""
                                           style="display: inline-block; width: 20px;height: 20px;margin-top: -2px"
                                           type="radio"
                                           value="1" ><span>立即发布</span>
                                </label>
                                <label onclick="releaseMethod(0)">
                                    <input class="form-control btn" id="releaseMethod_0"
                                           name="releaseMethod"
                                           placeholder=""
                                           style="display: inline-block; width: 20px;height: 20px;margin-top: -2px"
                                           type="radio"
                                           value="0"><span>延时发布</span>
                                </label>
                                <span class="cError"></span>
                            </td>
                            <th class="releaseTime" style="display: none"><span class="releaseTime"
                                                                                style="display: none"><span
                                    class="f_red">*</span>设置发布时间</span></th>
                            <td class="releaseTime" style="display: none">
                                <input class="form-control releaseTime" name="immediately" placeholder="" id="releasedDtime"
                                       style="display: none" type="datetime-local">
                                <span class="cError"></span>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="4" style="text-align: center">
                                <div class="bottom-tools-box" style="margin: auto">
                                    <button class="btn btn-info" id="save_but" onclick="addAndUpdateObject()"
                                            type="button"><span
                                            aria-hidden="true" class="glyphicon glyphicon-floppy-disk"></span> 发布
                                    </button>
                                    <button class="btn btn-warning" onclick="top.tab_colseOnclick();"
                                            type="button"><span
                                            aria-hidden="true" class="glyphicon glyphicon-remove-circle"></span> 取消
                                    </button>
                                </div>
                            </td>
                        </tr>
                    </table>
                </fieldset>


            </div>
        </div>
    </form>
</div>
<input th:id="id" th:value="${id}" type="hidden"/>
<script src="/static/vendor/jquery/jquery-3.3.1.min.js"></script>
<script src="/static/vendor/bootstrap/js/bootstrap.min.js"></script>
<script src="/static/vendor/skin/js/ft-ui.js"></script>
<script>
    $("form input,form select").blur(function () {
        let siblings = $(this).siblings(".cError");
        $(siblings).empty();
        if ($(this).val().trim() === "" || $(this).val() === null) {
            $(siblings).append(`
            <span style="font-size: 12px;font-weight: bold;color: red">不能为空</span>
            `)
            $(this).focus();
        }
    })
    function iniInfoForm() {
        let validator = $("#form1").validate({
            debug: true,
            rules: {
                // user_realname: {
                //     required: true
                // },
                // dq_yw_name: {
                //     required: true
                // }
            },
            messages: {
                // user_realname: {
                //     required: "执法人员不能为空！"
                // },
                // dq_yw_name: {
                //     required: "所属地区不能为空"
                // }
            },
            errorPlacement: function (error, element) {
                error.appendTo(element.parents('td').find(".cError"));
            },
            submitHandler: function (form) {
            }
        });
    }

    let id = $("#id").val();
    let iframe = document.getElementById("content");
    let editor = iframe.contentWindow.document;
    function p(s) {
        return s < 10 ? '0' + s : s;
    }
    function GetNowTime() {
        let myDate = new Date();
        let year = myDate.getFullYear();
        let month = myDate.getMonth() + 1;
        let date = myDate.getDate();
        let h = myDate.getHours();       //获取当前小时数(0-23)
        let m = myDate.getMinutes();     //获取当前分钟数(0-59) breedingManagement
        let s = myDate.getSeconds();
        return year + '-' + p(month) + "-" + p(date) + "T" + p(h) + ':' + p(m) + ":" + p(s);
    }
    $(function () {
        editor.designMode = "on";
        editor.contentEditable = true;
        $("#releasedDtime").val(GetNowTime());
        $("#title").focus();
        if (id !== null && id !== '') {
            $.get("/li/getObjByArgs?id=" + id, function (result) {
                $("#title").val(result[0].title);
                $('#content').contents().find('body').html(result[0].content);
                if (result[0].isPublic===0){
                    $('.releaseTime').show();
                    $("#releasedDtime").val(result[0].releasedDtime);
                    $("input[type=radio][value='0']").prop("checked", true);
                }else {
                    $("input[type=radio]").prop("disabled", true);
                    $("input[type=radio]").parent().attr("onclick","none")
                }
                $("#remark").val(result[0].remark);
            })
        }
    })
    let is_submit = false;
    let url = "";
    if (id !== null && id !== '') {
        url = '/li/updateLegalInfo';
    } else {
        url = '/li/addLegalInfo';
    }

    function addAndUpdateObject() {
        let time = $("#releasedDtime").val();
        let isPublic;
        let checked = $("input[type='radio'][name='releaseMethod']:checked").val();
       if (checked==='1'){
           isPublic=1;
           time=GetNowTime();
       }
        if (checked==='0'){
            isPublic=0
        }
        let bean = {
            id: id,
            title: $("#title").val(),
            content: $('#content').contents().find('body').html(),
            releasedDtime: time,
            isPublic:isPublic,
            remark: $("#remark").val(),
        }
        let submit = true;
        for (let beanKey in bean) {
            let beanElement = bean[beanKey];
            if ((beanElement === null || beanElement === "") && beanKey !== "id"&& beanKey !== "remark") {
                submit = false;
                $("#" + beanKey).siblings(".cError").empty().append(`<span style="font-size: 12px;font-weight: bold;color: red">不能为空</span>`);
                return;
            }
        }
        if (submit) {
            if (top.ajax.synclBody(url, bean)) {
                if (bean.isPublic===1){
                    this.send(bean.title)
                }
                modelWindow.alert("政策法规" + top.CMSLang.Add_success);
                top.getCurrentFrameObj().loadListble();
                top.tab_colseOnclick();
            } else {
                modelWindow.error("政策法规" + top.CMSLang.Add_fail);
            }

        }
    }

    function releaseMethod(val) {
        if (val === 0) {
            $('.releaseTime').show();
        } else {
            $('.releaseTime').hide();
        }
        $("input[type=radio][value=" + val + "]").prop("checked", true);
    }
    function setFontSize() {
        editor.execCommand('fontsize', true,$('select  option:selected').val());
    }
    $(".B").click(function () {
        editor.execCommand('bold', true);
    })
    $(".I").click(function () {
        editor.execCommand('italic', true);
    })
    $(".picture").click(function () {

    })

</script>
<script type="text/javascript">
    let websocket = null;
    if ('WebSocket' in window) {
        websocket = new WebSocket("ws://1.82.133.117:8083/websocket");
    } else {
        alert('Not support websocket')
    }
    //连接发生错误的回调方法
    websocket.onerror = function() {
    };
    //连接成功建立的回调方法
    websocket.onopen = function(event) {
    }
    //接收到消息的回调方法
    websocket.onmessage = function(event) {
    }
    //连接关闭的回调方法
    websocket.onclose = function() {
    }
    //监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
    window.onbeforeunload = function() {
        websocket.close();
    }
    //关闭连接
    function closeWebSocket() {
        websocket.close();
    }
    //发送消息
    function send(message) {
        websocket.send(message);
    }
</script>
</body>

</html>
